﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Business.WebsiteBusinessTagcode;
using BCDto.Common.Business.WebsiteBusinessTagcode;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.Business.WebsiteBusinessTagcode;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Business.WebsiteBusinessTagcode
{
    public class WebsiteBusinessTagcodeService : IWebsiteBusinessTagcodeService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteBusinessTagcodeData websiteBusinessTagcodeData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteBusinessTagcodeEntity);

        public WebsiteBusinessTagcodeService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteBusinessTagcodeData websiteBusinessTagcodeData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteBusinessTagcodeData = websiteBusinessTagcodeData;
        }

        #region 检查官网业务标记码是否存在
        public bool Exists(string websiteBusinessTagcode)
        {
            return this.websiteBusinessTagcodeData.Exists(websiteBusinessTagcode).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网业务标记码
        public WebsiteBusinessTagcodeDto Add(WebsiteBusinessTagcodeRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<WebsiteBusinessTagcodeEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            var result = this.websiteBusinessTagcodeData.Add(entity).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArgumentException("添加官网业务标记码失败!");
            }
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<WebsiteBusinessTagcodeDto>();
        }
        #endregion

        #region 修改官网业务标记码
        public WebsiteBusinessTagcodeDto Update(WebsiteBusinessTagcodePutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteBusinessTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WebsiteBusinessTagcodeEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteBusinessTagcodeData.Update(entity).GetAwaiter().GetResult().As<WebsiteBusinessTagcodeDto>();
            this.redisService.DeleteMultipleKey(key,false);
            return result;
        }
        #endregion

        #region 删除官网业务标记码
        public bool Delete(string websiteBusinessTagcode)
        {
            if (!this.Exists(websiteBusinessTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteBusinessTagcodeData.Delete(websiteBusinessTagcode).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取官网业务标记码
        public WebsiteBusinessTagcodeDto Get(string websiteBusinessTagcode)
        {
            string cacheKey = this.redisService.GetKey(key, websiteBusinessTagcode);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessTagcodeData.GetEntity(websiteBusinessTagcode).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteBusinessTagcodeDto>();
        }
        #endregion

        #region 获取官网业务标记码列表
        public IEnumerable<WebsiteBusinessTagcodeDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.websiteBusinessTagcodeData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteBusinessTagcodeDto>>();
        }
        #endregion

        #region 获取官网业务标记码分页
        public PageableList<WebsiteBusinessTagcodeDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessTagcodeData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteBusinessTagcodeDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteBusinessTagcodeDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网业务标记码
        public bool Enabled(string websiteBusinessTagcode)
        {
            if (!this.Exists(websiteBusinessTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessTagcodeData.Enabled(websiteBusinessTagcode).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 禁用官网业务标记码
        public bool Disabled(string websiteBusinessTagcode)
        {
            if (!this.Exists(websiteBusinessTagcode))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessTagcodeData.Disabled(websiteBusinessTagcode).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion
    }
}